package com.bds.wm.action;


import com.alibaba.fastjson.JSONObject;
import com.bds.wm.model.common.Result;
import com.bds.wm.model.procmodel.*;
import com.bds.wm.model.view.SubCategoryList;
import com.bds.wm.service.CityViewService;
import com.bds.wm.utils.CheckParams;
import com.bds.wm.utils.Const;
import com.google.common.collect.Lists;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: kevin
 * @date: 2018/5/18
 * @description:
 */
@Controller
public class CityViewAction {
    private static Logger LOGGER = Logger.getLogger(OverviewAction.class);


    @Resource
    private CityViewService cityViewService;


    @RequestMapping(value = "/service/data/get_city_view_result", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResult(String start_month,String end_month, String city_name) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null||endMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        CityViewResultByCity summaryResult = cityViewService.getSummaryByCity(city_name,startMonth,endMonth);
        if (summaryResult==null){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, summaryResult);
        }
        return result;
    }

    @RequestMapping(value = "/service/data/get_channel_per_result", method = RequestMethod.GET)
    @ResponseBody
    public Result getChannelPerResult(String start_month,String end_month, String city_name) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null||endMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyChannelTopCity> list = cityViewService.getChannelPerByCity(city_name,startMonth,endMonth);
        if (list==null||list.size()==0){
            result.setCode(Const.FAIL);
        }else{
            result.setResult(Const.SUC, list);
        }
        return result;
    }

    /**
     *
     * 不同平台情况
     * @param city
     * @param start_time
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/get_overviewresult_by_cityChannel", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultbyCityChannel(String city,String start_time, String end_time) {
        Result result = new Result();

        Integer start = CheckParams.checkIfIntAndReturn(start_time);
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (start == null || end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCityChannel> res= cityViewService.getOverviewResultbyCityChannel(city, start, end);
        result.setResult(Const.SUC, res);
        return result;
    }

    /**
     *
     * 促销信息
     * @param city
     * @param start_time
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/getPromotionInfobyCity", method = RequestMethod.GET)
    @ResponseBody
    public Result getPromotionInfobyCity(String city,String start_time, String end_time,String brandName) {
        Result result = new Result();

        Integer start = CheckParams.checkIfIntAndReturn(start_time);
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (start == null || end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<PromotionInfo> res= cityViewService.getPromotionInfobyCity(city, start, end,brandName);
        result.setResult(Const.SUC, res);
        return result;
    }

    /**
     *
     * 店铺品类销售情况
     * @param city
     * @param start_time
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/get_overviewresult_by_citySuCategory", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultbyCitySuCategory(String city,String start_time, String end_time) {
        Result result = new Result();
        Integer start = CheckParams.checkIfIntAndReturn(start_time);
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (start == null || end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCitySuCategory> cates = cityViewService.getOverviewResultbyCitySuCategory(city, start, end);
        JSONObject jsonObject = cityViewService.groupByCategory(cates);
        result.setResult(Const.SUC, jsonObject);
        return result;
    }

    @RequestMapping(value = "/service/data/getOverviewResultbyCitySubTier", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultbyCitySubTier(String cityTier,String start_time, String end_time) {
        Result result = new Result();
        Integer start = CheckParams.checkIfIntAndReturn(start_time);
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (start == null || end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCitySuCategory> cates = cityViewService.getOverviewResultbyCitySubTier(cityTier, start, end);
        JSONObject jsonObject = cityViewService.groupByCategory(cates);
        result.setResult(Const.SUC, jsonObject);
        return result;
    }
    /**
     * 返回12个月的统计数据 按SuCategory
     * @param city
     * @param suCategory
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/getOverviewResultTrendbyCitySuCategory", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultTrendbyCitySuCategory(String city,String suCategory, String end_time) {
        Result result = new Result();
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCitySuCategory> cates = cityViewService.getOverviewResultTrendbyCitySuCategory(city, suCategory, end);
        result.setResult(Const.SUC, cates);
        return result;
    }


    /**
     * 返回12个月的统计数据 按Category
     * @param city
     * @param category
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/getOverviewResultTrendbyCityCategory", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultTrendbyCityCategory(String city,String category, String end_time) {
        Result result = new Result();
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCitySuCategory> cates = cityViewService.getOverviewResultTrendbyCityCategory(city, category, end);
        result.setResult(Const.SUC, cates);
        return result;
    }


    /**
     * 返回12个月的统计数据 按section
     * @param city
     * @param section
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/getOverviewResultTrendbyCitySection", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultTrendbyCitySection(String city,String section, String end_time) {
        Result result = new Result();
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCitySuCategory> cates = cityViewService.getOverviewResultTrendbyCitySection(city, section, end);
        result.setResult(Const.SUC, cates);
        return result;
    }

    /**
     * 返回12个月的统计数据 按SuCategory
     * @param city
     * @param suCategory
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/getOverviewResultTrendbyCitySuTier", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultTrendbyCitySuTier(String city,String suCategory, String end_time) {
        Result result = new Result();
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCitySuCategory> cates = cityViewService.getOverviewResultTrendbyCitySuTier(city, suCategory, end);
        result.setResult(Const.SUC, cates);
        return result;
    }


    /**
     * 返回12个月的统计数据 按Category
     * @param city
     * @param category
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/getOverviewResultTrendbyCityCaTier", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultTrendbyCityCaTier(String city,String category, String end_time) {
        Result result = new Result();
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCitySuCategory> cates = cityViewService.getOverviewResultTrendbyCityCaTier(city, category, end);
        result.setResult(Const.SUC, cates);
        return result;
    }


    /**
     * 返回12个月的统计数据 按section
     * @param city
     * @param section
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/getOverviewResultTrendbyCitySeTier", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultTrendbyCitySeTier(String city,String section, String end_time) {
        Result result = new Result();
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCitySuCategory> cates = cityViewService.getOverviewResultTrendbyCitySeTier(city, section, end);
        result.setResult(Const.SUC, cates);
        return result;
    }

    /**
     *
     * 品牌销售情况
     * @param city
     * @param start_time
     * @param end_time
     * @return
     */
    @RequestMapping(value = "/service/data/get_overviewresult_by_cityBrand", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultbyCityBrand(String city,String start_time, String end_time,String subCategoryName,String brandName) {
        Result result = new Result();
        try {
            subCategoryName=URLDecoder.decode(subCategoryName, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Integer start = CheckParams.checkIfIntAndReturn(start_time);
        Integer end = CheckParams.checkIfIntAndReturn(end_time);
        if (start == null || end == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultbyCityBrand> cates = cityViewService.getOverviewResultbyCityBrand(city, start, end,subCategoryName,brandName);
        result.setResult(Const.SUC, cates);
        return result;
    }


    /**
     * 获取月度趋势折线图数据
     * @param end_time
     * @param month_num
     * @return
     */
    @RequestMapping(value = "/service/data/get_monthly_result_by_city_channel", method = RequestMethod.GET)
    @ResponseBody
    public Result getMonthlyResultByCityChannel(String city_name,String end_time, String month_num) {
        Result result = new Result();
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_time);
        Integer months = CheckParams.checkIfIntAndReturn(month_num);
        if (endMonth == null||months==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        JSONObject json=cityViewService.getMonthlyByCityChannel(city_name,endMonth,months);
        if (json==null){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, json);
        }
        return result;
    }


    /**
     * 获取复合增长率
     * @param start_month
     * @param end_month
     * @param city_name
     * @return
     */
    @RequestMapping(value = "/service/data/get_cagr_by_city", method = RequestMethod.GET)
    @ResponseBody
    public Result getCAGRbyCity(String start_month,String end_month, String city_name) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null||endMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        CAGRByCity cagr=cityViewService.getCAGRByCity(city_name,startMonth,endMonth);
        if (cagr==null){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, cagr);
        }
        return result;
    }


    @RequestMapping(value = "/service/data/get_city_map_overview_result", method = RequestMethod.GET)
    @ResponseBody
    public Result getCityMapOverviewResult(String start_month,String end_month, String city_name,String brand_name,
                                           String subcategory,String max_lng, String min_lng, String max_lat, String min_lat) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        Double maxLng = CheckParams.checkIfDoubleAndReturn(max_lng);
        Double minLng = CheckParams.checkIfDoubleAndReturn(min_lng);
        Double maxLat = CheckParams.checkIfDoubleAndReturn(max_lat);
        Double minLat = CheckParams.checkIfDoubleAndReturn(min_lat);
        if (startMonth == null||endMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth==null|| maxLng == null || minLng == null || maxLat == null || minLat == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        String brandName="";
        try {
            brandName = URLDecoder.decode(brand_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        city_name=city_name.replace("市","");
        List<CityMapOverviewResult> list=cityViewService.getCityMapOverviewResult(startMonth,endMonth,city_name,brandName,subcategory,
                max_lng,min_lng,max_lat,min_lat);
        if (list==null||list.size()==0){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, list);
        }
        return result;
    }

    /**
     * 获取同比、环比增长率
     */
    @RequestMapping(value = "/service/data/get_rate_by_city_sub", method = RequestMethod.GET)
    @ResponseBody
    public Result getRatebyCitySub(String start_month,String end_month, String city_name,String subcategory) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null||endMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        RatebyCitySub rate=cityViewService.getRatebyCitySub(city_name,startMonth,endMonth,subcategory);
        if (rate==null){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, rate);
        }
        return result;
    }

    /**
     * 获取城市排名
     */
    @RequestMapping(value = "/service/data/get_ranking_by_city", method = RequestMethod.GET)
    @ResponseBody
    public Result getCityRank(String start_month,String end_month, String city_name) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null||endMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        OverviewResultbyTopCityRaking rank=cityViewService.getTopCityRaking(city_name,startMonth,endMonth);
        if (rank==null){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, rank);
        }
        return result;
    }

    /**
     * 获取同比增长率
     * @param start_month
     * @param end_month
     * @param city_name
     * @return
     */
    @RequestMapping(value = "/service/data/get_yoy_overview_result_by_city", method = RequestMethod.GET)
    @ResponseBody
    public Result getYoYOverviewResultbyCity(String start_month,String end_month, String city_name,String selector) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null||endMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        YoYOverviewResultbyCity yoy=cityViewService.getYoYOverviewResultbyCity(city_name,startMonth,endMonth,selector);
        if (yoy==null){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, yoy);
        }
        return result;
    }

    /**
     * 增长最快品牌
     */
    @RequestMapping(value = "/service/data/getCityBrandGrowthRate", method = RequestMethod.GET)
    @ResponseBody
    public Result getCityBrandGrowthRate(String startMonth,String endMonth, String cityName,String subCategoryName) {
        Result result = new Result();
        Integer startMon = CheckParams.checkIfIntAndReturn(startMonth);
        Integer endMon = CheckParams.checkIfIntAndReturn(endMonth);
        if (startMonth == null||endMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<CityBrandGrowthRate> subCategoryNames=cityViewService.getCityBrandGrowthRate(cityName,startMon,endMon,subCategoryName);
        if (CollectionUtils.isEmpty(subCategoryNames)){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, subCategoryNames);
        }
        return result;
    }

    /**
     * 增长最快品牌 下拉框
     */
    @RequestMapping(value = "/service/data/getSubCategoryNames", method = RequestMethod.GET)
    @ResponseBody
    public Result getSubCategoryNames() {
        Result result = new Result();
        List<SubCategoryList> subCategoryNames = cityViewService.getSubCategoryNames();
        if (CollectionUtils.isEmpty(subCategoryNames)){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, subCategoryNames);
        }
        return result;
    }

    /**
     * 获得子类的店铺数、销售额、客单数
     */
    @RequestMapping(value = "/service/data/getOverviewResultbyCitySub", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultbyCitySub(String start_month,String end_month, String city_name,String subcate) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null||endMonth==null){
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        OverviewResultbyCitySub sub = cityViewService.getOverviewResultbyCitySub(startMonth,endMonth,city_name,subcate);
        if (sub==null){
            result.setCode(Const.FAIL);
        }else{
            result.setResult(Const.SUC, sub);
        }
        return result;
    }

    /**
     * 获得城市页下，西式快餐和Pizza&Pasta的各项排名
     */
    @RequestMapping(value = "/service/data/getTopCityRakingofSubcategory", method = RequestMethod.GET)
    @ResponseBody
    public Result getTopCityRakingofSubcategory(String start_month,String end_month, String city_name,String subcate) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null||endMonth==null){
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        TopCityRakingofSubcategory sub = cityViewService.getTopCityRakingofSubcategory(startMonth,endMonth,city_name,subcate);
        if (sub==null){
            result.setCode(Const.FAIL);
        }else{
            result.setResult(Const.SUC, sub);
        }
        return result;
    }
    @RequestMapping(value = "/service/data/getOverviewResultbyCityConsumption", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultbyCityConsumption(String city_name) {
        Result result = new Result();
        CityConsumption summaryResult = cityViewService.getOverviewResultbyCityConsumption(city_name);
        if (summaryResult==null){
            result.setCode(Const.FAIL);
        }else {
            result.setResult(Const.SUC, summaryResult);
        }
        return result;
    }



}
